/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package user;

import java.util.ArrayList;

/**
 *
 * @author northpole
 */
public class User {

    private boolean logged;
    private String username;
    private String interests;
    private String name;
    private String surname;
    private String dateOfBirth;
    private String welcomeText;
    private Connector conn;
    private String password;
    private int id;
    private ArrayList friendIds;
    private ArrayList friendUsernames;
    private ArrayList friendReqs;
    private int pendingFriendRequests;
    private miscMethods misc;

    public User() {
        this.logged = false;
        this.username = "Visitor";
        this.dateOfBirth = null;
        this.username = null;
        this.interests = null;
        this.name = null;
        this.surname = null;
        this.welcomeText = null;
        this.password = null;
        this.id = -1;
        this.friendIds = null;
        this.friendUsernames = null;
        this.pendingFriendRequests = 0;
        this.friendReqs = null;
        conn = new Connector();
        misc=new miscMethods(conn);
    }

    
            
    public String athenticate() {
        if (this.getConn() != null) {
            if (this.getConn().athenticate(this.getUsername(), this.getPassword()) == null) {
                this.logged=false;
                return "404";
            } else {
                logged=true;
            return "OK";}
        }
        return "404";

    }

   /* public String[] getFriends() {
        
        ArrayList friends = conn.getFriends(this.id);
        String[] result;
        
        if(friends==null)
            return null;
        
        friends.remove(this.username);
        this.friendUsernames = friends;
        result = new String[friends.size()];
        for (int i = 0; i < friends.size(); i++) {
            result[i] = friends.get(i).toString();
        }
        return result;
    }
*/
    //a friend is looking at the profile
    public void loadFriendInfo(){
    String []info=this.conn.getFriendsPublicInfo(username);
    this.setAll(info[0], info[1], info[3], info[2], info[4]);
    }
            
            //a stranger is loooking at the profile load public info
    public void loadPublicInfo(){


    String []info=this.conn.getPublicInfo(username); 
    this.setPublicInfo(info[0], info[1]);

   
    }

         public ArrayList  getFriendInfo(){
             this.loadFriendInfo();
         ArrayList result=new ArrayList(5);
         result.add(name);
         result.add(surname);
         result.add(dateOfBirth);
         result.add(interests);
         result.add(welcomeText);
         return result;
         }
            
        public ArrayList getPublicInfo(){
         ArrayList result=new ArrayList(5);
        result.add(this.interests);
         result.add(this.welcomeText);
         return result;
        }
        
        
            //load info for personnal use
    public void loadOwnInfo() {
        String info[] = null;
        if (this.isLogged()) {
            if (this.getUsername() != null) {
                info = this.getConn().getInfo(this.getUsername());
            }
            if (info != null && info.length == 5) {
                this.setAll(info[0], info[1], info[2], info[3], info[4]);//name,surname,interests,dateofbirth,welcometext
                this.getId();
            }
        } 
        
    }

    public void setAll(
            String name,
            String surname,
            String interests,
            String dateOfBirth,
            String welcomeText) {
        this.setDateOfBirth(dateOfBirth);
        this.setInterests(interests);
        this.setName(name);
        this.setSurname(surname);
        this.setWelcomeText(welcomeText);

    }

    public ArrayList getAll() {
        ArrayList result = new ArrayList();
        //result = new String[7];
        result.add(this.getUsername());
        result.add(this.getName());
        result.add(this.getSurname());
        result.add(this.getDateOfBirth());
        result.add(this.getInterests());
        result.add(this.getWelcomeText());
        Integer x = this.getId();
        result.add(x.toString());
        return result;
    }
    
    
    public boolean isFriend(String username)
    {  ArrayList sth=this.getFriendUsernames();
    if(sth==null)
        return false;
    if(sth.contains(username))
    return true;
       
    return false;
    }
    
    
    public int getId() {
        int id = 0;
        if (this.isLogged()) {
            id = this.conn.getId(this.getUsername());
            this.id = id;
        } else {
            id = -1;
        }

        return id;
    }

    /*load friend ids + usernames*/
    public void loadFriendList(){
        if ((this.isLogged()))
        friendIds=this.conn.getFriendIds(this.id);
        else
            return;
        
    friendUsernames=new ArrayList();
    Integer x=this.id;
        if(friendIds!=null)
        {friendIds.remove(x);
        friendUsernames=this.conn.getFriendUsernames(this.friendIds);
        friendUsernames.remove(this.username);}
      
    // if(friendUsernames==null)System.out.println("Friend usrenames==null");
    }
    
    
    public ArrayList getFriendIds(){return this.friendIds;}
    
    
    public ArrayList getFriendUsernames(){return this.friendUsernames;}
    
    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the interests
     */
    public String getInterests() {
        return interests;
    }

    /**
     * @param interests the interests to set
     */
    public void setInterests(String interests) {
        this.interests = interests;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the surname
     */
    public String getSurname() {
        return surname;
    }

    /**
     * @param surname the surname to set
     */
    public void setSurname(String surname) {
        this.surname = surname;
    }

    /**
     * @return the dateOfBirth
     */
    public String getDateOfBirth() {
        return dateOfBirth;
    }

    /**
     * @param dateOfBirth the dateOfBirth to set
     */
    public void setDateOfBirth(String dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    /**
     * @return the welcomeText
     */
    public String getWelcomeText() {
        return welcomeText;
    }

    /**
     * @param welcomeText the welcomeText to set
     */
    public void setWelcomeText(String welcomeText) {
        this.welcomeText = welcomeText;
    }

    /**
     * @return the conn
     */
    public Connector getConn() {
        return conn;
    }

    /**
     * @param conn the conn to set
     */
    public void setConn(Connector conn) {
        this.conn = conn;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    public void setId(int id) {
        this.id = id;
    }

    private void setFriendPublicInfo(String name, String surname, String dateofbirth) {
        this.setDateOfBirth(dateofbirth);
        this.setName(name);
        this.setSurname(surname);
    }

   /* public String[] getFriendPublicInfo(String friendsUsername) {
        String[] result;

        if (this.getFriendsus().contains(friendsUsername)) {
            result = new String[5];
            result[0] = this.getName();
            result[1] = this.getSurname();
            result[2] = this.getDateOfBirth();
            result[3] = this.getWelcomeText();
            result[4] = this.getInterests();
            return result;
        } else {
            return null;
        }
    }
*/
    private void setPublicInfo(String string, String string0) {
        this.setWelcomeText(string0);
        this.setInterests(string);
    }

   /* public String[] loadInfo(String friendsUsername) {
        String info[] = null;
        
        if (!friendsUsername.equals("Visitor"))
        if(this.isFriend(friendsUsername)) {
            info = this.conn.getFriendsPublicInfo(friendsUsername);
           
        }
        else
           info= this.conn.getPublicInfo(friendsUsername);
        return info;
    }*/


    public boolean pendingReq(String username2){
    boolean result=false;
    if(conn.reqExists(this.id,username2))
        result=true;
    return result;
    }
    
    public void deleteFriend(String username2){
    if(this.isFriend(username2))
        conn.deleteFriend(id, username2);
    }

    public void rejectRequest(String username2){
    if(conn.reqExists(this.id, username2))
        conn.rejectReq(this.id,username2);
    }

    public void addFriend(String friendsUsername) {
        //this.friendReqs.add(friendsUsername);
        this.setPendingFriendRequests(this.getPendingFriendRequests() + 1);
        getConn().addPendingReq(this.getId(), friendsUsername);
    }

    public void acceptFriend(String username) {
       
       // this.getFriendReqs().remove(username);
        //this.setPendingFriendRequests(this.getPendingFriendRequests() - 1);
        this.conn.acceptFriend(this.getId(), username);
    }

    public void loadFriendReqs() { if (!this.username.equals("Visitor")&& this.logged)
        this.setFriendReqs(this.getConn().loadFriendRequests(this.getId()));
        this.setPendingFriendRequests(this.getFriendReqs().size());
    }

    public String[] SuggestFriends() {
        String[] results = null;
        ArrayList sth = this.conn.suggestUsernames(this.username, this.id);
        results = new String[sth.size()];
        for (int i = 0; i < sth.size(); i++) {
            results[i] = sth.get(i).toString();
        }
        return results;
    }

    /**
     * @return the logged
     */
    public boolean isLogged() {
        return logged;
    }

    /**
     * @param logged the logged to set
     */
   /* public void setLogged(boolean logged) {
        this.logged = logged;
    }
*/
    /**
     * @param friends the friends to set
     */
   /* public void setFriends(ArrayList friends) {
        this.friends = friends;
    }
*/
    /**
     * @return the friendsus
     */
    public ArrayList getfriendUsernames() {
        return friendUsernames;
    }

    /**
     * @param friendUsernames the friendUsernames to set
     */
    public void setfriendUsernames(ArrayList friendUsernames) {
        this.friendUsernames = friendUsernames;
    }

    /**
     * @return the friendReqs
     */
    public ArrayList getFriendReqs() {
        return friendReqs;
    }

    /**
     * @param friendReqs the friendReqs to set
     */
    public void setFriendReqs(ArrayList friendReqs) {
        this.friendReqs = friendReqs;
    }

    /**
     * @return the pendingFriendRequests
     */
    public int getPendingFriendRequests() {
        return pendingFriendRequests;
    }

    /**
     * @param pendingFriendRequests the pendingFriendRequests to set
     */
    public void setPendingFriendRequests(int pendingFriendRequests) {
        this.pendingFriendRequests = pendingFriendRequests;
    }

    public String Register() {
        Integer x = -255;
        if (this.username.equals("Visitor")
                || this.dateOfBirth == null
                || this.interests == null
                || this.name == null
                || this.surname == null
                || this.password == null) {
            return "NeedMoarInfo";
        } else if (conn.findUsername(this.username) == null) {
            x = conn.SignUp(this);
            //  if( (x=conn.SignUp(this))==1)return "OK";
            /*else*/
            if (x == -1) {
                return x.toString();//x.toString();
            } else if (x == 1) {
                return "OK";
            }
        } else {
            return "Username Exists";
        }

        return x.toString();
    }

    public String updateInfo() {
       System.out.print(this.username);
        if(this.username!=null){
        if (this.username.equals("Visitor")
                || this.dateOfBirth == null
                || this.interests == null
                || this.name == null
                || this.surname == null
                || this.password == null) {
            return "Ilegal Access";
        } else if (conn.findUsername(this.username) != null) {
            int x = conn.updateProfile(this);
            if (x == 1) {
                return "OK";
            } else {
                return "Mysql Error";
            }
        }

        return null;
    }
 else
     throw new java.lang.AbstractMethodError(this.username);
    }


    public void deleteFRiend(String username) {
        if(!this.username.equals(username))
        this.conn.deleteFriend(this.id, username);
    }

    public void deleteProfile() {
        this.conn.deleteUser(this);
    }

    public ArrayList <Pm> getSentPms(){return this.conn.getSentPms(this.id);}
    public ArrayList <Pm> getReceivedPms(){return this.conn.getReceivedPms(this.id);}
    public ArrayList <Pm> getUnreadPms(){return this.conn.getUnreadPms(this.id);}
    public void readPm(Pm pm){this.conn.readPm(pm);}
    public void newPm(Pm pm){this.conn.createPm(pm);}

    public ArrayList getSuggestions(){return this.conn.getSuggestions(this.id);}

    public ArrayList pendingFriendRequestsTo(){return this.conn.getPendingFriendRequestsTo(this.username);}
    public ArrayList pendingFriendRequestsFrom(){return this.conn.pendingFriendRequestsFrom(this.username);}

    public int getFriendId(String username){
    return this.conn.getId(username);
    }
    public String getFriendUsername(int id){return this.conn.getUsername(id);}

    public miscMethods getMisc(){return misc;}
}
